<template>
  <el-dialog
      :title="type === 'add' ? '添加菜单' : '修改菜单'"
      v-model="visible"
      width="800px"
      @close="handleClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="menu-form">
      <el-form-item label="菜单名称" prop="name">
        <el-input type="text" v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="菜单url" prop="rank">
        <el-input type="text" v-model="ruleForm.url"></el-input>
      </el-form-item>
      <el-form-item label="菜单父级" prop="rank">
        <el-select  v-model="ruleForm.parentId" class="m-2" placeholder="Select" size="large">
          <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {reactive, ref, toRefs} from 'vue'
import {useRoute} from 'vue-router'
import axios from '@/utils/axios'
import {hasEmoji} from '@/utils'
import {ElMessage} from 'element-plus'

export default {
  name: 'DialogAddCategory',
  props: {
    type: String,
    reload: Function
  },
  setup(props) {
    const formRef = ref(null)
    const route = useRoute()
    const state = reactive({
      visible: false,
      options: [],
      ruleForm: {
        name: '',
        url: '',
        parentId: '',
      },
      rules: {
        name: [
          {required: 'true', message: '名称不能为空', trigger: ['change']}
        ],
      },
      option:{},
      id: ''
    })
    // 获取详情
    const getDetail = (id) => {
      return axios.get(`/evaluate/menu/${id}`).then(res => {
        state.ruleForm = {
          name: res.name,
          url: res.url,
          parentId: res.parentId
        }
      })
    }
    // 获取一级菜单
    const getLevel1Menu = async () => {
      return axios.get("/evaluate/menu").then(res => {
        state.options = res
        state.options.push({id: -1, name: "根目录"})
      })
    }

    // 开启弹窗
    const open = async (id) => {
      state.visible = true
      console.log(id)
      if (id) {
        state.id = id
        Promise.all([getDetail(id), getLevel1Menu()]).then(() => {
           console.log(state.options)
            state.option = state.options.filter(option=>option.id+"" === state.ruleForm.parentId+"")[0]
        })
      } else {
        state.ruleForm = {
          name: "",
          url: "",
          parentId: ''
        }
      }
    }
    // 关闭弹窗
    const close = () => {
      state.visible = false
    }
    const handleClose = () => {
      formRef.value.resetFields()
      state.options = []
    }
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          if (hasEmoji(state.ruleForm.name)) {
            ElMessage.error('不要输入表情包，再输入就打死你个龟孙儿~')
            return
          }
          if (state.ruleForm.name.length > 16) {
            ElMessage.error('名称不能超过16个字符')
            return
          }
          if (props.type === 'add') {
            axios.post('/evaluate/menu', state.ruleForm).then(() => {
              ElMessage.success('添加成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          } else {
            axios.post('/evaluate/menu', {
              ...state.ruleForm,
              id: state.id
            }).then(() => {
              ElMessage.success('修改成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          }
        }
      })
    }
    return {
      ...toRefs(state),
      open,
      close,
      formRef,
      submitForm,
      handleClose
    }
  }
}
</script>